﻿using Commerce_phuc.Models.Entity;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Commerce_phuc.Areas.Kuku.Controllers
{
    public class QuanAoController : BaseController
    {
        // GET: Kuku/QuanAo
        Entity db = new Entity();
       
        [HttpGet]
        public ActionResult Add()
        {
            var Category = db.Categorys
                    .Where(b => b.ParentID == 3);
            ViewBag.CategoryId = new SelectList(Category, "Id", "NameVN");
            return View();
        }
        [HttpPost]
        public ActionResult Add(ProductQuanAo model)
        {

            var f = Request.Files["UpImage"];
            if (!String.IsNullOrEmpty(f.FileName))
            {
                f.SaveAs(Server.MapPath("~/Images/QuanAo/" + f.FileName));
                model.Image = f.FileName;
            }
            try
            {
                model.CreatedDate = DateTime.Now;               
                db.ProductQuanAos.Add(model);
                db.SaveChanges();
                SetAlert("Thêm Thành Công", "success");
                ModelState.AddModelError("", "Cập nhật thành công !");
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                var Category = db.Categorys
                    .Where(b => b.ParentID == 3);
                ViewBag.CategoryId = new SelectList(Category, "Id", "NameVN");
                return View("Index", model);

            }

        }

        public ActionResult Edit(long id )
        {
           
            var Category = db.Categorys
                    .Where(b => b.ParentID == 3);
            ViewBag.CategoryId = new SelectList(Category, "Id", "NameVN");
            var maodel = db.ProductQuanAos.Find(id);
            return View(maodel);
        }
        [HttpPost]
        public ActionResult Edit(ProductQuanAo model)
        {
            var f = Request.Files["UpImage"];
            if (!String.IsNullOrEmpty(f.FileName))
            {
                System.IO.File.Delete(Server.MapPath("~/Images/QuanAo/" + model.Image));
                f.SaveAs(Server.MapPath("~/Images/QuanAo/" + f.FileName));
                model.Image = f.FileName;
            }
            try
            {
             
                model.ModifiedDate = DateTime.Now;
                db.Entry(model).State = EntityState.Modified;
                db.SaveChanges();
                SetAlert("Update Thành Công", "success");
                ModelState.AddModelError("", "Cập nhật Thành Công");

            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                var Category = db.Categorys
                    .Where(b => b.ParentID == 3);
                ViewBag.CategoryId = new SelectList(Category, "Id", "NameVN");

            }

            return RedirectToAction("Index", model);
        }
        public ActionResult Delete(long Id)
        {
            var model = db.ProductQuanAos.Find(Id);
            db.ProductQuanAos.Remove(model);
            db.SaveChanges();
            SetAlert("Xóa Sản phẩm Thành Công", "success");
            return RedirectToAction("Index");
        }
        [HttpGet]
        public ActionResult Index()
        {
            var a = db.ProductQuanAos
                    //.Where(b => b.Status == true)
                    ;

            ViewBag.ListProduct = db.ProductQuanAos.ToList();

            return View();
        }
        [HttpPost]
        public ActionResult Index(FormCollection formCollection)
        {
            string[] ids = formCollection["productId"].Split(new char[] { ',' });
            foreach (string id in ids)
            {
                var product = db.ProductQuanAos.Find(int.Parse(id));
                db.ProductQuanAos.Remove(product);
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }



    }
}